package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProducePassStationDetailRpcService;
import com.hunttown.mes.rpc.domain.MesProducePassStationDetailDTO;
import com.hunttown.mes.rpc.domain.query.MesProducePassStationDetailDTOQuery;
import com.hunttown.mes.service.MesProducePassStationDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProducePassStationDetailConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:31:09
 */
@Service
public class MesProducePassStationDetailRpcServiceImpl implements MesProducePassStationDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProducePassStationDetailRpcServiceImpl.class);

    private final MesProducePassStationDetailService mesProducePassStationDetailService;

    @Autowired
    public MesProducePassStationDetailRpcServiceImpl(MesProducePassStationDetailService mesProducePassStationDetailService) {
        this.mesProducePassStationDetailService = mesProducePassStationDetailService;
    }

    @Override
    public MesProducePassStationDetailDTO insert(MesProducePassStationDetailDTO mesProducePassStationDetailDTO) {
        try {
            return convert(mesProducePassStationDetailService.insert(convert(mesProducePassStationDetailDTO)));
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail create failed.", e);
            throw new RuntimeException("MesProducePassStationDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProducePassStationDetailDTO mesProducePassStationDetailDTO) {
        try {
            return mesProducePassStationDetailService.updateInfoById(convert(mesProducePassStationDetailDTO));
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail update by id failed.", e);
            throw new RuntimeException("MesProducePassStationDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProducePassStationDetailDTOQuery mesProducePassStationDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProducePassStationDetailDTOQuery.getW_id() != null && mesProducePassStationDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProducePassStationDetailDTOQuery.getW_id());
            }

            if (mesProducePassStationDetailDTOQuery.getIds() != null && !mesProducePassStationDetailDTOQuery.getIds().equals("")) {
                query.put("ids", mesProducePassStationDetailDTOQuery.getIds());
            }

            if (mesProducePassStationDetailDTOQuery.getW_packageId() != null) {
                query.put("w_packageId", mesProducePassStationDetailDTOQuery.getW_packageId());
            }

            if (mesProducePassStationDetailDTOQuery.getProduceId() != null) {
                query.put("produceId", mesProducePassStationDetailDTOQuery.getProduceId());
            }

            if (mesProducePassStationDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProducePassStationDetailDTOQuery.getDeleteFlag());
            }

            return mesProducePassStationDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail update by query failed.", e);
            throw new RuntimeException("MesProducePassStationDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProducePassStationDetailDTO selectById(Integer id) {
        try {
            return convert(mesProducePassStationDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail select by id failed.", e);
            throw new RuntimeException("MesProducePassStationDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProducePassStationDetailDTO selectObjByQuery(MesProducePassStationDetailDTOQuery mesProducePassStationDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProducePassStationDetailDTOQuery.getProduceId() != null) {
                query.put("produceId", mesProducePassStationDetailDTOQuery.getProduceId());
            }

            if (mesProducePassStationDetailDTOQuery.getPassId() != null) {
                query.put("passId", mesProducePassStationDetailDTOQuery.getPassId());
            }

            if (mesProducePassStationDetailDTOQuery.getPackageId() != null) {
                query.put("packageId", mesProducePassStationDetailDTOQuery.getPackageId());
            }

            if (mesProducePassStationDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProducePassStationDetailDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProducePassStationDetailDTOQuery.getOrderBy());
            }

            return convert(mesProducePassStationDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail select obj by query failed.", e);
            throw new RuntimeException("MesProducePassStationDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProducePassStationDetailDTO> getListForPage(MesProducePassStationDetailDTOQuery mesProducePassStationDetailDTOQuery) {
        Query query = Query.page(mesProducePassStationDetailDTOQuery.getPn(), mesProducePassStationDetailDTOQuery.getPageSize());

        if (mesProducePassStationDetailDTOQuery.getId() != null && mesProducePassStationDetailDTOQuery.getId() > 0) {
            query.put("id", mesProducePassStationDetailDTOQuery.getId());
        }

        if (mesProducePassStationDetailDTOQuery.getIds() != null && !mesProducePassStationDetailDTOQuery.getIds().equals("")) {
            query.put("ids", mesProducePassStationDetailDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getLikeName())) {
            query.put("likeName", mesProducePassStationDetailDTOQuery.getLikeName());
        }

        if (mesProducePassStationDetailDTOQuery.getProduceId() != null) {
            query.put("produceId", mesProducePassStationDetailDTOQuery.getProduceId());
        }

        if (mesProducePassStationDetailDTOQuery.getPassId() != null) {
            query.put("passId", mesProducePassStationDetailDTOQuery.getPassId());
        }

        if (mesProducePassStationDetailDTOQuery.getPackageId() != null) {
            query.put("packageId", mesProducePassStationDetailDTOQuery.getPackageId());
        }

        if (mesProducePassStationDetailDTOQuery.getPassStatus() != null) {
            query.put("passStatus", mesProducePassStationDetailDTOQuery.getPassStatus());
        }

        if (mesProducePassStationDetailDTOQuery.getHasBadCount() != null) {
            query.put("hasBadCount", mesProducePassStationDetailDTOQuery.getHasBadCount());
        }

        if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getShowDate())) {
            query.put("showDate", mesProducePassStationDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getStartDate())) {
            query.put("startDate", mesProducePassStationDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getEndDate())) {
            query.put("endDate", mesProducePassStationDetailDTOQuery.getEndDate());
        }

        if (mesProducePassStationDetailDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProducePassStationDetailDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProducePassStationDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProducePassStationDetailDTOQuery.getOrderBy());
        }

        List<MesProducePassStationDetailDTO> list = convert(mesProducePassStationDetailService.selectByQuery(query));
        Page<MesProducePassStationDetailDTO> page = Page.from(mesProducePassStationDetailDTOQuery.getPn(), mesProducePassStationDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProducePassStationDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProducePassStationDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProducePassStationDetail delete failed.", e);
            throw new RuntimeException("MesProducePassStationDetail delete failed, Please order your code.", e);
        }
    }
}